const Base = require('./base.js');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);

		this.reply = this.model("weixin/reply");
		this.conf = this.model("weixin/config");
		this.txt = this.model("weixin/txt");
		this.news = this.model("weixin/news");

	}
	async indexAction() {
		if(this.isGet) {
			let gets = this.get();
			let wxlist = await this.conf.select();
			this.assign("type", gets.type);
			this.assign("wxlist", wxlist);
			return this.display();
		}
		if(this.isPost) {
			let posts = this.post();
			let where = {

			};
			if(!think.isEmpty(posts.type)) {
				where.type = posts.type;
			}

			const data = await this.reply.dataTable(posts, where);
			console.log(data)
			return this.json(data);
		}

	}

	async addAction() {
		if(this.isGet) {
			let gets = this.get();
			let wxlist = await this.conf.select();
			this.assign("type", gets.type || 'follow');
			this.assign("wxlist", wxlist);
			return this.display();
		}
		if(this.isPost) {

			let posts = this.post();
			if(!posts.wxid) {
				return this.fail("请配置公众号");
			}

			let user = await this.session("user");
			let where = {
				wxid: posts.wxid
			};

			if(posts.type == 'follow') {
				where.type = posts.type;

			} else {
				where.keyword = posts.keyword;

			}

			let reply = await this.reply.where(where).find();
			if(!think.isEmpty(reply)) {
				return this.fail("自动回复已存在");
			}

			let insertId = await this.reply.add({
				wxid: posts.wxid,
				type: posts.type,
				keyword: posts.keyword,
				msgtype: posts.msgtype,
				content: posts.content,
				create_by: user.user_id,
				create_time: new Date().valueOf()

			});

			if(think.isEmpty(insertId)) {
				return this.fail("添加失败");
			}

			return this.success("添加成功");

		}
	}
	async editAction() {

		if(this.isGet) {
			let id = this.get("id");
			let reply = await this.reply.findOne(id);
			let wxlist = await this.conf.select();
			let content = [];
			if(reply.msgtype == 'txt') {
				content = await this.txt.findOne(reply.content);

			} else {
				content = await this.news.where({
					id: ['IN', reply.content]
				}).select();
			}

			this.assign("reply", reply);
			this.assign("wxlist", wxlist);
			this.assign("contents", content);
			return this.display();
		}
		if(this.isPost) {

			let posts = this.post();

			let update = await this.reply.setOne(posts.id, {
				wxid: posts.wxid,
				type: posts.type,
				keyword: posts.keyword,
				msgtype: posts.msgtype,
				content: posts.content

			});

			if(update == 0) {
				return this.fail("修改失败");
			}

			return this.success("修改成功");

		}

	}

	async selectAction() {
		if(this.isGet) {
			let gets = this.get();
			this.assign("msgtype", gets.msgtype);
			return this.display();
		}
		if(this.isPost) {
			let posts = this.post();

			let where = {};
			let data = {};

			if(posts.msgtype == 'txt') {
				data = await this.txt.dataTable(posts, where);
			} else {
				data = await this.news.dataTable(posts, where);
			}

			console.log(data)
			return this.json(data);
		}

	}

};